package com.example.springboot02amqp.controller;

import com.example.springboot02amqp.model.MailBean;
import com.example.springboot02amqp.util.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

/**
 * Created with IntelliJ IDEA.
 * User: 喵喵~
 * Date: 2022/3/22
 * Time: 10:30
 * Description:
 */
@RestController
public class MailController {
    @Autowired
    MailUtil mailUtil;
    @Autowired
    private TemplateEngine emailTemplateEngine;

    @PostMapping("/mail")
    public void sendEmail(){
        MailBean mailBean = new MailBean();
        mailBean.setSubject("赛默飞test");
        mailBean.setToAccount("2274165222@qq.com");
        //thymeleaf模版解析成String
        Context ctx = new Context();
        ctx.setVariable("orderCode", "88888888");
        String htmlContent = emailTemplateEngine.process("msg.html", ctx);
        mailBean.setContent(htmlContent);
        mailUtil.sendMail(mailBean);

        System.out.println("success!");
    }
    @PostMapping("/html-email")
    public void sendHtmlEmail(){
        MailBean mailBean = new MailBean();
        mailBean.setSubject("赛默飞test");
        mailBean.setToAccount("2274165222@qq.com");
        //thymeleaf模版解析成String
        Context ctx = new Context();
        ctx.setVariable("orderCode", "88888888");
        String htmlContent = emailTemplateEngine.process("msg.html", ctx);
        mailBean.setContent(htmlContent);
        mailUtil.sendTemplateMail(mailBean.getToAccount(),mailBean.getSubject(),htmlContent);

        System.out.println("success!");
    }
}
